package com.tengju.bff.interfaces.manage.crm.user.assembler;

import com.tengju.bff.interfaces.manage.crm.user.vo.MarketRateVO;
import com.tengju.bff.interfaces.manage.crm.user.vo.MarketTypeVO;
import com.tengju.bff.interfaces.shared.AmountType;
import com.tengju.bff.interfaces.shared.AmountVO;
import com.tengju.bff.interfaces.shared.MobileVO;
import com.tengju.data.domain.model.clickhouse.OdsMarketIndex;
import com.tengju.data.domain.model.report.AchievementBonus;
import com.tengju.data.domain.model.report.TeamRatioType;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author chenhang
 * @Date 2021/1/26
 */
public class CrmMarketAssembler {
    public static MarketTypeVO bonusToTypeVO(AchievementBonus bonus, BigDecimal totalAchievement) {
        if (bonus == null) {
            return null;
        }
        MarketTypeVO marketTypeVO = new MarketTypeVO();
        BigDecimal achievement = bonus.getAchievement().compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : bonus.getAchievement();
        BigDecimal profit = bonus.getProfit().compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : bonus.getProfit();
        marketTypeVO.setMarketIndex(new AmountVO(AmountType.INDEX, achievement));
        marketTypeVO.setAdIndex(new AmountVO(AmountType.INDEX, profit));
        double rate = 0;
        if (totalAchievement.compareTo(BigDecimal.ZERO) != 0) {
            rate = bonus.getAchievement().divide(totalAchievement, 4, RoundingMode.HALF_DOWN).doubleValue();
        }
        marketTypeVO.setMarketRate(new AmountVO(AmountType.PERCENT, rate));
        TeamRatioType teamRatioType = TeamRatioType.parseByType(bonus.getTeamRatioType());
        marketTypeVO.setTeamRatioType(bonus.getTeamRatioType());
        if (teamRatioType != null) {
            marketTypeVO.setTypeName(teamRatioType.getName());
        } else {
            marketTypeVO.setTypeName(bonus.getTeamRatioType() + "");
        }
        return marketTypeVO;
    }

    public static MarketRateVO assemblerToRateVO(OdsMarketIndex marketIndex, BigDecimal totalAchievement) {
        if (marketIndex == null) {
            return null;
        }
        MarketRateVO marketRateVO = new MarketRateVO();
        marketRateVO.setIdCode(marketIndex.getIdCode().getValue());
        marketRateVO.setMarketIndex(new AmountVO(AmountType.INDEX, marketIndex.getAchievement()));
        marketRateVO.setAdIndex(new AmountVO(AmountType.INDEX, marketIndex.getProfit()));
        String name = null;
        if (StringUtils.isNotBlank(marketIndex.getTengjuNickname())) {
            name = marketIndex.getTengjuNickname();
        }
        if (StringUtils.isBlank(name) && StringUtils.isNotBlank(marketIndex.getAbmNickname())) {
            name = marketIndex.getAbmNickname();
        }
        marketRateVO.setName(name);

        String mobile = null;
        if (StringUtils.isNotBlank(marketIndex.getTengjuMobile())) {
            mobile = marketIndex.getTengjuMobile();
        }
        if (StringUtils.isBlank(mobile) && StringUtils.isNotBlank(marketIndex.getAbmMobile())) {
            mobile = marketIndex.getAbmMobile();
        }
        marketRateVO.setMobile(new MobileVO(mobile).toString());

        marketRateVO.setStarLevel(marketIndex.getStarLevel().getCode());
        marketRateVO.setStarLevelDesc(marketIndex.getStarLevel().getDesc());

        double rate = 0;
        if (totalAchievement.compareTo(BigDecimal.ZERO) != 0) {
            rate = marketIndex.getAchievement().divide(totalAchievement, 4, RoundingMode.HALF_DOWN).doubleValue();
        }
        marketRateVO.setRate(new AmountVO(AmountType.PERCENT, rate).getValue());
        return marketRateVO;
    }
}
